Ext.define('Zm.controller.Tour', {
    extend: 'Ext.app.Controller',
    uses:['Zm.Common','Zm.i18n.Lang'],
	stores:['Tour','TourLog','TourLogItem'],
    refs:[
        {ref:'X3dView', selector:'x3dview'},
        {ref:'X3dContainer',selector:'x3dcontainer'},
        {ref:'CardView', selector:'cardview'},
        {ref:'List', selector:'tourlist'}
    ],
    views:['tour.Edit','tour.SelectTour','tour.LogItems'],
    init: function(){
        var me = this;
        me.control({
            'tourlist button[action=add]':{
                click:me.onAdd
            },
            'tourlist button[action=del]':{
                click:me.onDel
            },
            'tourlist button[action=edit]':{
                click:me.onEdit
            },
            'tourlist':{
                itemdblclick:me.onEdit
            },
            'tourposition button[action=add]':{
                click:me.onAddPos
            },
            'touredit button[action=save]':{
                click:me.doSave
            },
            'selecttour button[action=save]':{
                click:me.startTour
            },
            'tourlog button[action=search]':{
                click:me.searchLog
            },
            'tourlog':{
                itemdblclick:me.showLogItem
            }
        });
        me.application.on({
            pagechange:me.showPage,
            scope:me
        });
	},
    showPage:function(id){
        var me = this,view;
        if(id == 'tour'){
            me.getCardView().getLayout().setActiveItem(me.getX3dContainer());      
            view = Ext.widget('selecttour');
        }
    },
    startTour:function(btn){
        var me = this,
            win = btn.up('window'),
            frm = win.down('form'),
            x3dview = me.getX3dView(),
            form = frm.getForm();
        form.submit({
            url: g_app + '/Tour/startTour',
            scope: me,
            success: function(form,action){
                var data = action.result.data;
                x3dview.objTourPos.setAttribute('key',data.key);
                x3dview.objTourPos.setAttribute('keyValue',data.pos);
                x3dview.objTourOri.setAttribute('key',data.key);
                x3dview.objTourOri.setAttribute('keyValue',data.ori);
                x3dview.objTourVp.setAttribute('set_bind','true');
                x3dview.objTourTimer.setAttribute('startTime', new Date().getTime()/1000 );
                x3dview.objTourTimer.setAttribute('cycleInterval', data.timelen );
                win.close();
            },
            failure: Zm.Common.onFail
        });
    },
    onAdd:function(btn){
        var view = Ext.widget('touredit');
    },
    onDel:function(btn){
        var me = this,rd,
            lst =  me.getList(),
            sm = lst.getSelectionModel(),
            records = sm.getSelection(),
            lang = Zm.i18n.Lang;
        if(records.length>0){
            rd = records[0];
            Ext.MessageBox.show({
                   title:lang.tDel,
                   msg: Ext.String.format(lang.tDelTourMsg,rd.get('tourname')),
                   buttons: Ext.Msg.YESNO,
                   fn: me.onDelCallback,
                   scope: me,
                   tid:rd.get('tid'),
                   icon: Ext.Msg.QUESTION
            });                                        
        }
    },
    onDelCallback: function(btn,text,opt){
        var me = this,
            lang = Zm.i18n.Lang,
            tid = opt.tid,
            cm = Zm.Common;
        if(btn == 'yes'){
            Ext.Ajax.request({
                url: g_app + '/Tour/del',
                params: {tid:tid},
                scope: me,
                success:function(response,options){ 
                    me.getTourStore().load();
                },
                failure:cm.onAFail
             });    
        }
    },
    onEdit:function(btn){
        var me = this,
            sm = me.getList().getSelectionModel(),
            records = sm.getSelection(),
            posList,itemList,
            view;
        if(records.length>0){
            view = Ext.widget('touredit');   
            view.down('form').loadRecord(records[0]);
            posList = view.down('#poslist');
            if(posList) posList.setValue(records[0].get('tid'));
            itemList = view.down('#itemlist');
            if(itemList) itemList.setValue(records[0].get('tid'));
        }        
    },
    
    doSave:function(btn){
        var me = this,
            win = btn.up('window'),
            frm = win.down('form'),
            form = frm.getForm(),
            posList = frm.down('#poslist'),
            itemList = frm.down('#itemlist'),
            posVal,itemVal;
        if(posList) posVal = posList.getValue();
        if(itemList) itemVal = itemList.getValue();
        form.submit({
            url: g_app + '/Tour/save',
            params:{
                pos:Ext.encode(posVal),
                items:Ext.encode(itemVal)
            },
            scope: me,
            success: function(form,action){
                me.getTourStore().load();
                win.close();
                me.getCardView().getLayout().setActiveItem(me.getList());
            },
            failure: Zm.Common.onFail
        });
    },
    onAddPos:function(btn){
        var me = this,
            vp = me.getX3dView().originalVP,
            view = btn.up('tourposition'),
            rd = Ext.create('Zm.model.Position');
        rd.set('position',vp.position);
        rd.set('orientation',vp.orientation);
        view.store.add(rd);
    },
    searchLog:function(btn){
        var me = this,
            loglist = btn.up('tourlog'),
            startdate = loglist.down('#startdate').getValue(),
            enddate = loglist.down('#enddate').getValue(),
            tid = loglist.down('#tid').getValue(),
            proxy = loglist.store.getProxy();
        proxy.extraParams = {startdate:startdate,enddate:enddate,tid:tid};
        loglist.store.loadPage(1);
    },
    showLogItem:function(view,record){
        var dlg = Ext.widget('tourlogitems');
        dlg.setValue(record.get('logid'));
    }
});